<?php
                                                                                    
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('fmfrmpromotion_loadGrid');      
$objXajax->registerFunction('fmfrmpromotion_save');
$objXajax->registerFunction('fmfrmpromotion_edit'); 

function fmfrmpromotion_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix,$hotel_id;
    $objResponse =  new xajaxResponse();
    $name = $params['txtname'];
    $type = $params['type'];
    $description = addslashes($params['txtdescription']);
    $percent = $params['txtpercent'];    
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE fm_promotion SET name = '$name',type = '$type',description = '$description',
            percent = '$percent' WHERE id =$id"; 
        $objDbSelect->Execute($sql);
        
    }else{
          $sql= " INSERT INTO fm_promotion(name,percent,type,description,service_id,active,flg_promotion) VALUES('$name','$percent','$type','$description',$hotel_id,1,1)"; 
          $objDbSelect->Execute($sql); 
     }    
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_fmfrmpromotion_loadGrid();'); 
    $objResponse->addScript("hasChange= true;");    
    return $objResponse->getXML();
}


function fmfrmpromotion_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = fmfrmpromotion_getGrid(); 
    //echo '<pre>'; print_r($arrGrid); die();   
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/fm_promotion_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmpromotion_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmpromotion_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function fmfrmpromotion_getGrid()
{
    global $objDbSelect,$lang,$hotel_id;
    $sqlSelect = " select * from fm_promotion where service_id =  $hotel_id and flg_promotion= 1";                                                     
    return $objDbSelect->GetArray($sqlSelect);
}  

    function fmfrmpromotion_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM fm_promotion WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name       = $arr[0]['name'];
        $type       = $arr[0]['type'];    
        $percent    = $arr[0]['percent'];
        
        $description =htmlspecialchars_decode($arr[0]['description']) ;
        $objResponse->addAssign('fmfrmpromotion_txtdescription','value',$description);
        $objResponse->addScript("$('#fmfrmpromotion_txtname').val('$name');");
        $objResponse->addScript("$('#fmfrmpromotion_txtpercent').val('$percent');");
        $objResponse->addScript("$('#fmfrmpromotion_type$type').attr('checked','checked');");      
        
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
?>
